package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.ScaleType;
import org.yscz.entity.vo.ScaleTypeVo;
import org.yscz.service.ScaleTypeService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 量表分类接口
 *
 * @author muqs
 * @create 2021-08-19 18:45
 */
@RestController
@Slf4j
@Api("量表分类接口")
public class ScaleTypeController {

    @Autowired
    private ScaleTypeService scaleTypeService;

    /**
     * 新增量表分类接口
     *
     * @param scaleType 前台传递的量表分类数据
     * @return Result对象
     */
    @PostMapping("/scaleType/add")
    @ApiOperation(value = "新增量表分类接口", httpMethod = "POST")
    public Result add(@RequestBody ScaleType scaleType) {
        log.info("scaleType:[{}]", scaleType);
        scaleTypeService.add(scaleType);
        return Result.success(HttpStatus.SUCCESS, "新增量表分类成功", "");
    }

    /**
     * 修改量表分类接口
     *
     * @param scaleType 前台传递的量表分类数据
     * @return Result对象
     */
    @PostMapping("/scaleType/modify")
    @ApiOperation(value = " 修改量表分类接口", httpMethod = "POST")
    public Result modify(@RequestHeader(value = "scaleTypeName", required = false) String scaleTypeName,
                         @RequestBody ScaleType scaleType) {
        log.info("scaleType:[{}]", scaleType);
        if (!StringUtils.isEmpty(scaleTypeName)) {
            scaleType.setScaleTypeEditor(scaleTypeName);
        }
        scaleTypeService.modify(scaleType);
        return Result.success(HttpStatus.SUCCESS, "修改量表分类成功", "");
    }

    /**
     * 删除量表分类接口
     *
     * @param scaleTypeId 前台传递的量表分类数据
     * @return Result对象
     */
    @DeleteMapping("/scaleType/del/{scaleTypeId}")
    @ApiOperation(value = " 删除量表分类接口", httpMethod = "DELETE")
    public Result del(@PathVariable Long scaleTypeId) {
        log.info("scaleTypeId:[{}]", scaleTypeId);
        scaleTypeService.del(scaleTypeId);
        return Result.success(HttpStatus.SUCCESS, "删除量表分类成功", "");
    }

    /**
     * 查询量表分类列表接口(分页)
     *
     * @param scaleTypeName 量表分类名
     * @param pageNum       当前页
     * @param pageSize      每页数据条数
     * @return Result对象
     */
    @GetMapping("/scaleType/listPage")
    @ApiOperation(value = "查询量表分类接口(分页)", httpMethod = "GET")
    public Result listPage(@RequestParam(value = "scaleTypeName", required = false) String scaleTypeName,
                           @RequestParam(value = "type", required = false, defaultValue = "1") Integer type,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        JSONObject reusltObj = new JSONObject();
        IPage<ScaleTypeVo> page = new Page(pageNum, pageSize);
        Map condition = new HashMap<>();
        condition.put("type", type);
        //QueryWrapper queryWrapper = new QueryWrapper();

        if (!StringUtils.isEmpty(scaleTypeName)) {
            //queryWrapper.like("scaleType_name", scaleTypeName);
            condition.put("scaleTypeName", "%" + scaleTypeName + "%");
        }

        IPage<ScaleType> scaleTypePage = scaleTypeService.listPageByCondition(page, condition);

        reusltObj.put("list", scaleTypePage == null ? new ArrayList<>() : scaleTypePage.getRecords());
        reusltObj.put("total", scaleTypePage == null ? 0 : scaleTypePage.getTotal());

        return Result.success(HttpStatus.SUCCESS, "查询量表分类列表(分页)成功", reusltObj);
    }

    /**
     * 查询量表分类列表接口
     *
     * @return Result对象
     */
    @GetMapping("/scaleType/list")
    @ApiOperation(value = "查询量表分类接口", httpMethod = "GET")
    public Result list() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("scale_type_sort");
        List<ScaleType> scaleTypes = scaleTypeService.list(queryWrapper);

        return Result.success(HttpStatus.SUCCESS, "查询量表分类列表成功", scaleTypes);
    }
}
